//
// Created by zhfayuan on 2021/6/9.
//

#ifndef GBRELAYERVER_RELAYERMINAL_H
#define GBRELAYERVER_RELAYERMINAL_H

#include <memory>
#include <thread>
#include <mutex>
#include <condition_variable>

#include "SDMedia.h"
#include "SDPlayback.h"

class RelayStreamHandler
{
public:
    virtual  void onRelayProviderOffPosition(uint64_t uid,uint64_t room,uint64_t index) = 0;
    virtual  void onRelayProviderOnPosition(uint64_t uid,uint64_t room,uint64_t index) = 0;
    virtual  void onRelayAudioData(uint64_t id,char *data,uint32_t length,uint64_t  pts) = 0;
    virtual  void onRelayVideoData(uint64_t id,char *data,uint32_t length,uint64_t  pts,bool key,bool hevc) = 0;
};

class RelayTermHandler
{
public:
    virtual void onRelayConnectionLost() = 0;
};

class RelayTerminal:public CSDMediaIFace
{
public:
    RelayTerminal(RelayTermHandler *t,RelayStreamHandler *m);
    virtual  ~RelayTerminal();
private:
    RelayTermHandler *_tHandler;
    RelayStreamHandler *_mHandler;
    std::shared_ptr<CSDMedia> _inner;
protected:
    virtual void OnSystemExit(UINT unExitCause) ;
    virtual void OnRemoteVideo(UINT64 unRoomId,BYTE byCodec,BYTE byIndex, BYTE* data, UINT unLen, UINT unPTS, VideoFrameInfo tFrameInfo);
    virtual void OnRemoteAudio(UINT64 unRoomId,BYTE byCodec,BYTE byIndex, BYTE* data, UINT unLen, UINT unPTS, AudioFrameInfo tFrameInfo) ;
    virtual void OnRemoteCmd(USHORT cmd,USHORT subCmd,BYTE*data,UINT unLen) ;
    virtual void OnKeyNegotiated(UINT unKeyToken,BYTE*key,UINT unKeyLen);
    virtual void OnStreamDecryptFailed(UINT64 unRoomId, BYTE byIndex);
    virtual void OnFileExtractResult(std::string strPath,BYTE toEnd,UINT unExitCause) ;
    virtual void OnFileExtractProgress(std::string strPath,BYTE percent) ;
    virtual void OnAVProviderOnPosition(UINT64 unUid,UINT64 unRoomId,BYTE byAvIndex) ;
    virtual void OnAVProviderOffPosition(UINT64 unUid,UINT64 unRoomId,BYTE byAvIndex);
public:
    void disconnect();
    bool connect(uint32_t gid, std::string ip,uint32_t port);
    bool onPosition(uint64_t room);
    void offPosition(uint64_t room);
};

#if 0
class RelayPlaybackStreamHandler
{
public:
    //virtual  void onRelayPlaybackAudioData(uint64_t id,char *data,uint32_t length,uint64_t  pts) = 0;
    //virtual  void onRelayPlaybackVideoData(uint64_t id,char *data,uint32_t length,uint64_t  pts,bool key,bool hevc) = 0;
};

class RelayPlaybackHandler
{
public:
    virtual void onRelayPlaybackConnectionLost() = 0;
};

class RelayPlayback:public CSDPlaybackIFace
{
public:
    RelayPlayback(RelayPlaybackHandler *t,RelayPlaybackStreamHandler *m);
    virtual  ~RelayPlayback();
private:
    RelayPlaybackHandler *_tHandler;
    RelayPlaybackStreamHandler *_mHandler;
    std::shared_ptr<CSDPlayback> _inner;
protected:
    virtual void OnCompleted(UINT unAvMsgIndex);
    virtual void OnOpened(UINT unAvMsgIndex,BYTE success);
    virtual void OnSystemExit(UINT unExitCause) ;
    virtual void OnVodPlayProgress(UINT unAvMsgIndex, UINT  sec, UINT duration) ;
    virtual void OnRemoteVideo(UINT unAvMsgIndex,BYTE byCodec,BYTE byIndex, BYTE* data, UINT unLen, UINT unPTS, VideoFrameInfo tFrameInfo) ;
    virtual void OnRemoteAudio(UINT unAvMsgIndex,BYTE byCodec,BYTE byIndex, BYTE* data, UINT unLen, UINT unPTS, AudioFrameInfo tFrameInfo) ;
    virtual void OnStreamDecryptFailed(UINT unAvMsgIndex, BYTE byIndex) ;
public:
    void disconnect();
    bool connect(std::string ip,uint32_t port);
};
#endif


#endif //FACESERVER_FACETERMINAL_H
